import { ITEM_DATA } from '../../data/item';
import {
  REWARD_TYPE,
  type ExpReward,
  type ItemReward,
  type Reward,
  type RewardConfig,
  type SectContributionReward,
  type SpiritStoneReward,
} from './type';

/**
 * 奖励创建器类
 */
export class RewardFactory {
  /**
   * 创建战斗经验值值奖励
   */
  static createExpReward(config: RewardConfig): ExpReward {
    if (config.amount === undefined) {
      throw new Error('战斗经验值奖励必须提供 amount 参数');
    }

    return {
      type: REWARD_TYPE.EXP,
      name: config.name || '战斗经验值',
      description: config.description,
      amount: config.amount,
    };
  }

  /**
   * 创建物品奖励
   */
  static createItemReward(config: Partial<RewardConfig>): ItemReward {
    if (config.itemId === undefined) {
      throw new Error('物品奖励必须提供 itemId 参数');
    }
    const itemConfig = ITEM_DATA.find((item) => item.id === config.itemId);
    if (!itemConfig) {
      throw new Error(`未找到 ID 为 "${config.itemId}" 的物品配置`);
    }
    if (config.quantity === undefined) {
      throw new Error('物品奖励必须提供 quantity 参数');
    }

    return {
      type: REWARD_TYPE.ITEM,
      name: config.name || itemConfig.name,
      description: config.description,
      itemId: config.itemId,
      quantity: config.quantity,
    };
  }

  /**
   * 创建灵石奖励
   */
  static createSpiritStoneReward(
    config: Partial<RewardConfig>
  ): SpiritStoneReward {
    if (config.quantity === undefined) {
      throw new Error('灵石奖励必须提供 quantity 参数');
    }

    return {
      type: REWARD_TYPE.SPIRIT_STONE,
      name: config.name || '灵石',
      description: config.description,
      quantity: config.quantity,
    };
  }

  /**
   * 创建宗门贡献奖励
   */
  static createSectContributionReward(
    config: Partial<RewardConfig>
  ): SectContributionReward {
    if (config.quantity === undefined) {
      throw new Error('宗门贡献奖励必须提供 quantity 参数');
    }

    return {
      type: REWARD_TYPE.SECT_CONTRIBUTION,
      name: config.name || '宗门贡献',
      description: config.description,
      quantity: config.quantity,
    };
  }

  /**
   * 根据配置创建奖励
   */
  static createReward(config: RewardConfig): Reward {
    switch (config.type) {
      case REWARD_TYPE.EXP:
        return this.createExpReward(config);

      case REWARD_TYPE.ITEM:
        return this.createItemReward(config);

      case REWARD_TYPE.SPIRIT_STONE:
        return this.createSpiritStoneReward(config);

      case REWARD_TYPE.SECT_CONTRIBUTION:
        return this.createSectContributionReward(config);

      default:
        throw new Error(`不支持的奖励类型: ${config.type}`);
    }
  }

  /**
   * 批量创建奖励
   */
  static createRewards(configs: RewardConfig[]): Reward[] {
    return configs.map((config) => this.createReward(config));
  }
}
